package com.cy2;

import java.util.Scanner;

/**
 * The type Username exception.
 *
 * @author CY
 * @date 2022 /08/13 22:05
 */
class UsernameException extends Exception {
    /**
     * Instantiates a new Username exception.
     */
    public UsernameException() {

    }

    /**
     * Instantiates a new Username exception.
     *
     * @param s the s
     */
    public UsernameException(String s) {
        super(s);
    }
}

/**
 * The type Password exception.
 */
class PasswordException extends Exception {
    /**
     * Instantiates a new Password exception.
     */
    public PasswordException() {

    }

    /**
     * Instantiates a new Password exception.
     *
     * @param s the s
     */
    public PasswordException(String s) {
        super(s);
    }
}

/**
 * The type Login.
 */
public class Login {
    /**
     * Input boolean.
     *
     * @param username the username 用户名
     * @param password the password  密码
     * @return the boolean
     * @throws UsernameException the username exception
     * @throws PasswordException the password exception
     */
    public static boolean input(String username, String password) throws UsernameException, PasswordException {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的用户名：");
        String s1 = sc.nextLine();
        if (!s1.equals(username)) {
            throw new UsernameException("用户名输入错误");
        }
        System.out.println("请输入你的密码：");
        String s2 = sc.nextLine();
        if (!s2.equals(password)) {
            throw new PasswordException("密码输入错误!");
        }
        return true;
    }

    /**
     * The entry point of application.
     *
     * @param args the input arguments
     */
    public static void main(String[] args) {
        String username = "Admin";
        String password = "123456";
        boolean ret = false;
        int times = 3;
        System.out.println("你还有" + times + "次机会！");
        while (true) {
            try {
                ret = input(username, password);
            } catch (UsernameException e) {
                e.printStackTrace();
                times--;
            } catch (PasswordException e) {
                e.printStackTrace();
                times--;
            } finally {
                if (ret || times == 0) {
                    if (ret) {
                        System.out.println("登录成功！");
                    } else {
                        System.out.println("登录失败，你被强制退出！");
                    }
                    break;
                } else {
                    System.out.println("你还有" + times + "次机会！");
                }
            }
        }
    }
}
